package example.bill.com.jnilearn;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnUICallNative;
    private Button btnStartJavaThread;
    private Button btnStartNativeThread;
    private Thread javaThread;
    private static final String TAG ="JNILearn";

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    private void initView() {
        // Example of a call to a native method
        btnUICallNative = (Button)findViewById(R.id.btnUICallNative);
        btnStartJavaThread = (Button)findViewById(R.id.btnStartJavaThread);
        btnStartNativeThread = (Button)findViewById(R.id.btnStartNativeThread);

        btnUICallNative.setOnClickListener(this);
        btnStartJavaThread.setOnClickListener(this);
        btnStartNativeThread.setOnClickListener(this);
    }

    private void initData() {

        EventBus.getDefault().register(this);

        javaThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while(true){

                    Log.i(TAG," call native function current thread  " + Thread.currentThread().getId());
                    testJNIEnv();

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private native void testJNIEnv();
    private native void startNativeThread();

    @Override
    public void onClick(View v) {
        int resId = v.getId();
        switch (resId){

            case R.id.btnUICallNative:
                /*
                 * UI线程调native方法
                 * */
                Log.i(TAG,"call native method current thread " + Thread.currentThread().getId());
                testJNIEnv();

                break;

            case R.id.btnStartJavaThread:
                /*
                * 开启java层的线程，并调用native方法
                * */
                javaThread.start();
                break;

            case R.id.btnStartNativeThread:
                /*
                * 开启native层的线程，并回调java层的方法
                * */
                startNativeThread();

                break;
        }
    }

    /**
     * 注意，处理事件的函数必须是public的
     * */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public  void processEventInfo(EventInfo eventInfo){
        Log.i(TAG," process event info  " + eventInfo.toString()
                + " in MainThread " + Thread.currentThread().getId());
    }


    @Override
    protected void onDestroy(){
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
        super.onDestroy();
    }
}
